<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Example: ScrollView with Scroll Indicator and Link Suppression Behavior</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>Example: ScrollView with Scroll Indicator and Link Suppression Behavior</h1>

    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
    <p>This example shows how to create a ScrollView widget with a scrollbar indicator.  It also illustrates a technique for suppressing link behavior during a scroll &mdash; a technique you may require if you have a ScrollView instance heavily populated by links, as in this example.</p>
</div>

<div class="example newwindow">
    <a href="scrollview-example.html" target="_blank" class="button">
        View Example in New Window
    </a>
</div>

<h2>The ScrollView Widget With A Scroll Indicator</h2>

<p>In this example, we'll create a ScrollView instance, which also has a scrollbar indicator.</p>

<h3>Modules Used</h3>

<p>Since we want to use the base ScrollView, along with the ScrollViewScrollbars plugin, which provides the scrollbar indicator we use the <code>scrollview</code> module, instead of the <code>scrollview-base</code> module we used for the basic ScrollView example:</p>

<pre class="code prettyprint">&#x2F;&#x2F; Pulls in scrollview-base and scrollview-scrollbars plugin 
&#x2F;&#x2F; and plugs it in (at the class level)

YUI().use(&#x27;scrollview&#x27;, function(Y) {
    ...
});</pre>


<p>The <code>scrollview</code> module pulls in the basic ScrollView and also the ScrollViewScrollbars plugin. It has code which plugs the scrollbar plugin into the ScrollView base class:</p>

<pre class="code prettyprint">Y.Base.plug(Y.ScrollView, Y.Plugin.ScrollViewScrollbars);</pre>


<h3>Instantiating The ScrollView Widget</h3>

<p>As with the <a href="scrollview-base.html">Base ScrollView</a> example, we provide the markup for the ScrollView content on the page, as shown below:</p>

<pre class="code prettyprint">&lt;div id=&quot;scrollview-content&quot; class=&quot;yui3-scrollview-loading&quot;&gt;
    &lt;ul&gt;
        &lt;li&gt;AC&#x2F;DC&lt;&#x2F;li&gt;
        &lt;li&gt;Aerosmith&lt;&#x2F;li&gt;
        &lt;li&gt;Billy Joel&lt;&#x2F;li&gt;
        &lt;li&gt;Bob Dylan&lt;&#x2F;li&gt;
        ...
    &lt;&#x2F;ul&gt;
&lt;&#x2F;div&gt;</pre>


<p>And we instantiate the ScrollView instance in the same way, providing the <code>srcNode</code> attribute during construction, so it uses the markup above for it's content:</p>

<pre class="code prettyprint">YUI().use(&#x27;scrollview&#x27;, function(Y) {

    var scrollView = new Y.ScrollView({
        id: &quot;scrollview&quot;,
        srcNode: &#x27;#scrollview-content&#x27;,
        height: 310,
        flick: {
            minDistance:10,
            minVelocity:0.3,
            axis: &quot;y&quot;
        }
    });

    scrollView.render();
});</pre>


<p>Again, for this example, since we want a vertically scrolling ScrollView widget, we also give it a height during construction and constrain flicks to the "y" axis.</p>

<p>As the last step, to see the functional ScrollView on the page, we call <code>scrollView.render()</code>.</p>

<p>The only difference, compared to the <a href="scrollview-base.html">Base ScrollView</a> example, is that the ScrollViewScrollbars plugin has been pulled down and plugged in by the <code>scrollview</code> module code shown above, so the ScrollView now has a scroll indicator. The scroll indicator is styled with rounded corners in browsers which support CSS rounded corners natively.</p>

<h3>Accessing The Scrollbars Plugin API</h3>

<p>As discussed in the <a href="index.html#scrollbars">ScrollBar Plugin</a> documentation, the API to control scrollbars is available on the scrollview instance, through the <code>scrollView.scrollbars</code> property. The ScrollBar plugin doesn't have too complex of an api, just a few methods to hide and show the scrollbars:</p>

<pre class="code prettyprint">&#x2F;* 
  scrollView.scrollbars is an instance of the ScrollViewScrollbars plugin 
*&#x2F;
scrollView.scrollbars.hide();
scrollView.scrollbars.show(); 
scrollView.scrollbars.flash();
});</pre>


<h3>Suppressing Default Link Behavior</h3>

<p>In this example, the scrolling surface is populated with links. To prevent links' default action (page navigation) from taking place after a scroll, we look at the <code>lastScrolledAmt</code> property of our ScrollView instance; on a simple click, that number will be very close to zero, whereas after scroll that number will be much higher.  In this case, we're using a 2px threshold.</p>

<pre class="code prettyprint">var content = scrollView.get(&quot;contentBox&quot;); 

content.delegate(&quot;click&quot;, function(e) {
    &#x2F;&#x2F; Prevent links from navigating as part of a scroll gesture
    if (Math.abs(scrollView.lastScrolledAmt) &gt; 2) {
        e.preventDefault();
        Y.log(&quot;Link behavior suppressed.&quot;)
    }
}, &quot;a&quot;);</pre>


<p>We also prevent default on mousedown, to prevent the native "drag link to desktop" behavior on certain browsers.</p>

<pre class="code prettyprint">content.delegate(&quot;mousedown&quot;, function(e) {
    &#x2F;&#x2F; Prevent default anchor drag behavior, on browsers 
    &#x2F;&#x2F; which let you drag anchors to the desktop
    e.preventDefault();
}, &quot;a&quot;);</pre>


<h2>Complete Example Source</h2>
<pre class="code prettyprint">&lt;div id=&quot;scrollview-container&quot;&gt;
    &lt;div id=&quot;scrollview-header&quot;&gt;
        &lt;h1&gt;ScrollView&lt;&#x2F;h1&gt;
    &lt;&#x2F;div&gt;
    &lt;div id=&quot;scrollview-content&quot; class=&quot;yui3-scrollview-loading&quot;&gt;
        &lt;ul&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.acdc.com&#x2F;&quot; title=&quot;AC&#x2F;DC | The Official AC&#x2F;DC Site&quot;&gt;AC&#x2F;DC&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.aerosmith.com&#x2F;&quot; title=&quot;Aerosmith | Aerosmith -&quot;&gt;Aerosmith&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.billyjoel.com&#x2F;&quot; title=&quot;Billy Joel | The Official Billy Joel Site&quot;&gt;Billy Joel&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.bobdylan.com&#x2F;&quot; title=&quot;Home Page | Bob Dylan&quot;&gt;Bob Dylan&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.bobseger.com&#x2F;&quot; title=&quot;Bob Seger | Official Site | Home&quot;&gt;Bob Seger&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.bonjovi.com&#x2F;&quot; title=&quot;Bon Jovi &amp;#134; The Official Website&quot;&gt;Bon Jovi&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.brucespringsteen.net&#x2F;&quot; title=&quot;BRUCE SPRINGSTEEN IS AWESOME&quot;&gt;Bruce Springsteen&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.creed.com&#x2F;&quot; title=&quot;Creed.com – The Official Website of Creed&quot;&gt;Creed&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Creedence_Clearwater_Revival&quot; title=&quot;Creedence Clearwater Revival - Wikipedia, the free encyclopedia&quot;&gt;Creedence Clearwater Revival&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.davematthewsband.com&#x2F;&quot; title=&quot;DAVE MATTHEWS BAND | Home Page | Dave Matthews Band&quot;&gt;Dave Matthews Band&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.defleppard.com&#x2F;news&#x2F;&quot; title=&quot;DefLeppard.com | the official Def Leppard web site&quot;&gt;Def Leppard&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.eaglesband.com&#x2F;&quot; title=&quot;Eagles | eaglesband.com&quot;&gt;The Eagles&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
					&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.bunnymen.com&#x2F;&quot; title=&quot;Echo &amp;amp; The Bunnymen&quot;&gt;Echo and the Bunnymen&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.eminem.com&#x2F;&quot; title=&quot;Eminem : Official Site&quot;&gt;Eminem&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.fleetwoodmac.com&#x2F;&quot; title=&quot;Fleetwood Mac&quot;&gt;Fleetwood Mac&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;Green Day&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.gunsnroses.com&#x2F;&quot; title=&quot;Guns N&#x27; Roses: Home: Chinese Democracy&quot;&gt;Guns N&#x27; Roses&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.jamestaylor.com&#x2F;&quot; title=&quot;James Taylor: The Official Site&quot;&gt;James Taylor&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.jay-z.com&#x2F;index.php&quot; title=&quot;Jay-Z.com: The Official Jay-Z Website&quot;&gt;Jay-Z&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Jimi_Hendrix&quot; title=&quot;Jimi Hendrix - Wikipedia, the free encyclopedia&quot;&gt;Jimi Hendrix&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.ledzeppelin.com&#x2F;&quot; title=&quot;Led Zeppelin - Official Website&quot;&gt;Led Zeppelin&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.lynyrdskynyrd.com&#x2F;&quot; title=&quot;Lynyrd Skynyrd&quot;&gt;Lynyrd Skynyrd&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;metallica.com&#x2F;&quot; title=&quot;Metallica.com&quot;&gt;Metallica&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;Motley Crue&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.neildiamond.com&#x2F;&quot; title=&quot;Official site from Neil Diamond&quot;&gt;Neil Diamond&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Nirvana_(band)&quot; title=&quot;Nirvana (band) - Wikipedia, the free encyclopedia&quot;&gt;Nirvana&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.ozzy.com&#x2F;&quot; title=&quot;Ozzy Osbourne | The Official Ozzy Osbourne Site&quot;&gt;Ozzy Osbourne&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;pearljam.com&#x2F;&quot; title=&quot;Home | Pearl Jam - Ten Club&quot;&gt;Pearl Jam&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.pinkfloyd.com&#x2F;&quot; title=&quot;Pink Floyd | The Official Site&quot;&gt;Pink Floyd&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.queenonline.com&#x2F;&quot; title=&quot;Queen Online.com&quot;&gt;Queen&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.rodstewart.com&#x2F;&quot; title=&quot;Rod Stewart | The Official Rod Stewart Site&quot;&gt;Rod Stewart&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.rush.com&#x2F;&quot; title=&quot;RUSH - Official Website&quot;&gt;Rush&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.santana.com&#x2F;&quot; title=&quot;SANTANA - The Official Carlos Santana Web Site&quot;&gt;Santana&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.simonandgarfunkel.com&#x2F;&quot; title=&quot;Simon &amp;amp; Garfunkel official site&quot;&gt;Simon and Garfunkel&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.stevemillerband.com&#x2F;&quot; title=&quot;Steve Miller Band - Official Web Site&quot;&gt;Steve Miller Band&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.thebeatles.com&#x2F;&quot; title=&quot;The Beatles&quot;&gt;The Beatles&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.thedoors.com&#x2F;&quot; title=&quot;The Doors&quot;&gt;The Doors&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.thepolicefile.com&#x2F;&quot; title=&quot;ThePoliceFile.com: The Police - Concert Tickets - World Tour - The Police&quot;&gt;The Police&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.rollingstones.com&#x2F;&quot; title=&quot;The Official site of the Rolling Stones | Rollingstones.com&quot;&gt;The Rolling Stones&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.tompetty.com&#x2F;&quot; title=&quot;TomPetty.com &amp;gt; Home&quot;&gt;Tom Petty&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.u2.com&#x2F;&quot; title=&quot;U2 &amp;gt; Welcome&quot;&gt;U2&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.van-halen.com&#x2F;&quot; title=&quot;Van-Halen.com | the official Van Halen web site&quot;&gt;Van Halen&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.willienelson.com&#x2F;&quot; title=&quot;Home &amp;raquo; 
            			Willie Nelson&quot;&gt;Willie Nelson&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
            &lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.zztop.com&#x2F;&quot; title=&quot;ZZ Top | Home&quot;&gt;ZZ Top&lt;&#x2F;a&gt;&lt;&#x2F;li&gt; 
        &lt;&#x2F;ul&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;div&gt;

&lt;div id=&quot;additional-content&quot;&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.&lt;&#x2F;p&gt;
    &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras aliquam hendrerit elit id vulputate. Pellentesque pellentesque erat rutrum velit facilisis sodales convallis tellus lacinia. Curabitur gravida mi sit amet nulla suscipit sed congue dolor volutpat. Aenean sem tortor, pretium et euismod in, imperdiet sit amet urna. Ut ante nisi, auctor mattis suscipit a, ullamcorper eget leo. Phasellus sagittis ante at lectus rutrum ut sollicitudin sem malesuada. Duis ultrices sapien et nulla tincidunt malesuada. Mauris ante turpis, dignissim eu tincidunt vitae, placerat quis diam. In augue nisl, cursus at rutrum ut, scelerisque et erat. Suspendisse potenti. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris orci dui, aliquam ut convallis ut, dapibus et erat. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aliquam erat volutpat. Mauris placerat elit id lectus rhoncus in dignissim justo mollis. Donec nec odio sapien. In iaculis euismod felis non laoreet. Mauris ornare varius neque, et congue erat porta a. Aliquam nec auctor lectus. Etiam ut ipsum a nibh iaculis fringilla.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;

&lt;script type=&quot;text&#x2F;javascript&quot; charset=&quot;utf-8&quot;&gt;
    YUI().use(&#x27;scrollview&#x27;, function(Y) {

        var scrollView = new Y.ScrollView({
            id: &quot;scrollview&quot;,
            srcNode: &#x27;#scrollview-content&#x27;,
            height: 310,
            flick: {
                minDistance:10,
                minVelocity:0.3,
                axis: &quot;y&quot;
            }
        });

        scrollView.render();
        
        var content = scrollView.get(&quot;contentBox&quot;); 

				content.delegate(&quot;click&quot;, function(e) {
				    &#x2F;&#x2F; Prevent links from navigating as part of a scroll gesture
				    if (Math.abs(scrollView.lastScrolledAmt) &gt; 2) {
				        e.preventDefault();
						Y.log(&quot;Link behavior suppressed.&quot;)
				    }
				}, &quot;a&quot;);

        content.delegate(&quot;mousedown&quot;, function(e) {
            &#x2F;&#x2F; Prevent default anchor drag behavior, on browsers which let you drag anchors to the desktop
            e.preventDefault();
        }, &quot;a&quot;);
    });

&lt;&#x2F;script&gt;</pre>

</div>
        </div>

        <div id="sidebar" class="yui3-u">
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="This example creates a basic ScrollView which doesn&#x27;t include a scrollbar indicator.">
                                        <a href="scrollview-base.html">Basic ScrollView Without a Scroll Indicator</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example shows the classic Scrollview implementation, including scroll indicators (bars) and including code to suppress link navigation while scrolling.">
                                        <a href="scrollview.html">ScrollView with Scroll Indicator and Link Suppression Behavior</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example creates a horizontal ScrollView.">
                                        <a href="scrollview-horiz.html">Horizontal ScrollView</a>
                                    </li>
                                
                            
                                
                                    <li data-description="This example creates a horizontal ScrollView with pagination support.">
                                        <a href="scrollview-paging.html">ScrollView With Pagination</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
